<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar title="文章详情" left-arrow fixed placeholder>
      <!-- 后退按钮 -->
      <template #left>
        <van-icon
          name="arrow-left"
          size="0.48rem"
          @click="$router.back()"
          color="white"
        />
      </template>
    </van-nav-bar>
    <!-- loading 图标 -->
    <van-loading size="0.64rem" color="#0094ff"  text-color="#0094ff" vertical v-if="!articleInfo.title">文章螺旋加载中...</van-loading>
    <!-- 文章信息区域 -->
    <div class="article-container" v-else>
      <!-- 文章标题 -->
      <h1 class="art-title">{{ articleInfo.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="articleInfo.aut_name"
        :label="formatDate(articleInfo.pubdate)"
      >
        <template #icon>
          <img :src="articleInfo.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="!articleInfo.is_followed"
              @click="followedFn(articleInfo.aut_id)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="infollowedFn(articleInfo.aut_id)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleInfo.content" v-hightlight></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="articleInfo.attitude === 1"
          @click="attitudeFn(articleInfo.art_id)"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="inAttitudeFn(articleInfo.art_id)"
          >点赞</van-button
        >
      </div>
      <commentList :artId="this.$route.query.value"></commentList>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn' // import locale
import * as relativeTime from 'dayjs/plugin/relativeTime'
import {
  getArticleInfo,
  userFollowApi,
  userInFollowApi,
  inAttitudeApi,
  attitudeApi
} from '@/api'
import commentList from '@/views/search/articleDetail/commentList.vue'

dayjs.locale('zh-cn') // use locale
dayjs().format()
dayjs.extend(relativeTime)
export default {
  name: 'Article',
  components: {
    commentList
  },
  data () {
    return {
      articleInfo: []
    }
  },
  async created () {
    const res = await getArticleInfo(this.$route.query.value)
    this.articleInfo = res.data.data
  },
  methods: {
    formatDate (data) {
      var a = dayjs()
      var b = dayjs(data)

      const c = a.to(b) // in 2 years
      return c
    },
    async followedFn (value) {
      this.articleInfo.is_followed = true
      await userFollowApi(value)
    },
    async infollowedFn (value) {
      this.articleInfo.is_followed = false
      await userInFollowApi(value)
    },
    async attitudeFn (val) {
      this.articleInfo.attitude = -1
      await inAttitudeApi(val)
    },
    async inAttitudeFn (val) {
      this.articleInfo.attitude = 1
      await attitudeApi(val)
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
